package service.impl;

import dao.BookDao;
import dao.LendDao;
import dao.UserDao;
import dao.impl.BookDaoImpl;
import dao.impl.LendDaoImpl;
import dao.impl.UserDaoImpl;
import entity.Book;
import service.BookService;

import java.sql.SQLException;
import java.util.List;

public class BookServiceImpl implements BookService {
    BookDao bookDao=new BookDaoImpl();

    public Book queryBookById(String bookId) {
        try {
            if(bookDao.isExist(bookId)){
                Book book=bookDao.queryBookById(bookId);
                return book;
            }else{
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Book> queryAllBooks() {
        try {
            List<Book> books=bookDao.queryAllBooks();
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



    public boolean updateBookById(String bookId, Book book) {
        try {
            if(bookDao.isExist(bookId)){
                if(bookDao.updateBookById(bookId,book)){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteBookById(String bookId) {
        try {
            if(bookDao.isExist(bookId)){
                if(!bookDao.isIn(bookId)){
                    return false;
                }
                else{
                    if(bookDao.deleteBookById(bookId)){
                        return true;
                    }else{
                        return false;
                    }
                }
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean addBook(Book book) {
        try {
            if(bookDao.isExist(book.getBookId())){
                return false;
            }else{
                if(bookDao.addBook(book)){
                    return true;
                }else{
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public int getBookTotalCount(String sql,Object[] params) {
        try {
            int count=bookDao.getTotalCount(sql,params);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public List<Book> queryBookByPage(int currentPage, int pageSize) {
        try {
            List<Book> books=bookDao.queryBookByPage(currentPage,pageSize);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Book> queryBookByPageRequire(int currentPage,int pageSize,Object param){
        try {
            List<Book> books=bookDao.queryBookByPageRequire(currentPage,pageSize,param);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Book> queryBorrowBookByPage(int currentPage,int pageSize){
        try {
            List<Book> books=bookDao.queryBorrowBookByPage(currentPage,pageSize);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Book> queryBorrowBookByPageRequire(int currentPage,int pageSize,Object param){
        try {
            List<Book> books=bookDao.queryBorrowBookByPageRequire(currentPage,pageSize,param);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Book> queryReturnBookByPage(int currentPage,int pageSize,String userId) {
        try {
            List<Book> books=bookDao.queryReturnBookByPage(currentPage,pageSize,userId);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据书籍信息查询可归还书籍当前页的数据集合 currentPage(当前页码) pageSize(页面大小)
    public List<Book> queryReturnBookByPageRequire(int currentPage,int pageSize,String userId,Object param) {
        try {
            List<Book> books=bookDao.queryReturnBookByPageRequire(currentPage,pageSize,userId,param);
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) {
//        Book book=new Book("斗破苍穹","20220002","笔趣阁",99,"天蚕土豆","true", Util.getDate());
//        System.out.println(new BookServiceImpl().addBook(book));
//        System.out.println(new BookServiceImpl().queryBookById("20220001").getBookId());
//        List<Book> books=new BookServiceImpl().queryAllBooks();
//        for(Book book:books){
//            System.out.println(book.getBookId());
//        }
//        Book book=new Book("斗破苍穹","20220002","笔趣阁",100,"天蚕土豆","true", Util.getDate());
//        System.out.println(new BookServiceImpl().updateBookById("20200002",book));
//        System.out.println(new BookServiceImpl().deleteBookById("20220001"));
//        System.out.println(new BookServiceImpl().getBookTatalCount("select count(*) from books"));
//        List<Book> books=new BookServiceImpl().queryBookByPage(0,2);
//        for(Book book:books){
//            System.out.println(book.getBookId());
//        }
//    }
}
